﻿package com.sgcc.nfc.lock.presentation.rooms

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.sgcc.nfc.lock.R
import com.sgcc.nfc.lock.databinding.ItemRoomBinding
import com.sgcc.nfc.lock.domain.model.Room
import com.sgcc.nfc.lock.domain.model.RoomStatus

class RoomAdapter(
    private val onItemClick: (Room) -> Unit
) : ListAdapter<Room, RoomAdapter.RoomViewHolder>(DiffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RoomViewHolder {
        val binding = ItemRoomBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return RoomViewHolder(binding)
    }

    override fun onBindViewHolder(holder: RoomViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class RoomViewHolder(
        private val binding: ItemRoomBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Room) {
            val ctx = binding.root.context
            binding.roomNameText.text = item.roomName
            binding.roomAddressText.text = item.address ?: "-"
            binding.roomCountsText.text = ctx.getString(R.string.dashboard_total_locks, item.lockCount)
            
            // 更新状态芯片
            binding.roomStatusChip.apply {
                text = when (item.status) {
                    RoomStatus.NORMAL -> ctx.getString(R.string.room_status_safe)
                    RoomStatus.IN_USE -> ctx.getString(R.string.room_status_in_use)
                    RoomStatus.ALERT -> ctx.getString(R.string.room_status_alert)
                    RoomStatus.UNKNOWN -> ctx.getString(R.string.lock_status_unknown)
                }
                
                // 设置芯片颜色
                val colorRes = when (item.status) {
                    RoomStatus.NORMAL -> R.color.color_status_success
                    RoomStatus.IN_USE -> R.color.color_status_warning
                    RoomStatus.ALERT -> R.color.color_status_error
                    RoomStatus.UNKNOWN -> android.R.color.darker_gray
                }
                chipBackgroundColor = ContextCompat.getColorStateList(ctx, colorRes)
                setTextColor(ContextCompat.getColor(ctx, android.R.color.white))
            }
            
            // 保持旧的TextView隐藏但设置内容以保持兼容性
            binding.roomStatusText.apply {
                text = when (item.status) {
                    RoomStatus.NORMAL -> ctx.getString(R.string.room_status_safe)
                    RoomStatus.IN_USE -> ctx.getString(R.string.room_status_in_use)
                    RoomStatus.ALERT -> ctx.getString(R.string.room_status_alert)
                    RoomStatus.UNKNOWN -> ctx.getString(R.string.lock_status_unknown)
                }
            }
            
            binding.root.setOnClickListener { onItemClick(item) }
        }
    }

    companion object DiffCallback : DiffUtil.ItemCallback<Room>() {
        override fun areItemsTheSame(oldItem: Room, newItem: Room): Boolean = oldItem.roomId == newItem.roomId
        override fun areContentsTheSame(oldItem: Room, newItem: Room): Boolean = oldItem == newItem
    }
}
